using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.Loon.Framework.Xna.Game.Core;
using Org.Loon.Framework.Xna.Game.Core.Timer;
using Org.Loon.Framework.Xna.Game.Core.Graphics.OpenGL;
using Org.Loon.Framework.Xna.Game.Core.Geom;

namespace Org.Loon.Framework.Xna.Game.Action.Sprite.Effect
{
    public class ScaleEffect : LObject, ISprite
    {

        private int width, height;

        private bool visible, complete;

        private LTimer timer;

        private LTexture texture;

        private int count;

        private int maxcount = 20;

        private int centerX, centerY;

        private bool flag;

        public ScaleEffect(String fileName, bool f)
            : this(new LTexture(fileName), f)
        {

        }

        public ScaleEffect(LTexture t, bool f)
        {
            this.texture = t;
            this.width = t.Width;
            this.height = t.Height;
            this.timer = new LTimer(100);
            this.visible = true;
            if (f)
            {
                this.count = maxcount;
                this.flag = f;
            }
        }

        public void SetDelay(long delay)
        {
            timer.SetDelay(delay);
        }

        public long GetDelay()
        {
            return timer.GetDelay();
        }

        public bool IsComplete()
        {
            return complete;
        }

        public override int GetHeight()
        {
            return height;
        }

        public override int GetWidth()
        {
            return width;
        }

        public override void Update(long elapsedTime)
        {
            if (complete)
            {
                return;
            }
            if (flag)
            {
                if (this.count <= 0)
                {
                    this.complete = true;
                }
                if (timer.Action(elapsedTime))
                {
                    count--;
                }
            }
            else
            {
                if (this.count >= this.maxcount)
                {
                    this.complete = true;
                }
                if (timer.Action(elapsedTime))
                {
                    count++;
                }
            }
        }

        public void CreateUI(GLEx g)
        {
            if (!visible)
            {
                return;
            }
            if (complete)
            {
                if (!flag)
                {
                    if (alpha > 0 && alpha < 1)
                    {
                        g.SetAlpha(alpha);
                    }
                    g.DrawTexture(this.texture, this.X(), this.Y(), GetWidth(), GetHeight());
                    if (alpha > 0 && alpha < 1)
                    {
                        g.SetAlpha(1f);
                    }
                }
                return;
            }
            if (this.centerX < 0)
            {
                this.centerX = (GetWidth() / 2);
            }
            if (this.centerY < 0)
            {
                this.centerY = (GetHeight() / 2);
            }
            float partx = this.centerX / this.maxcount;
            float party = this.centerY / this.maxcount;
            float partWidth = (GetWidth() - this.centerX) / this.maxcount;
            float partHeight = (GetHeight() - this.centerY) / this.maxcount;
            int x = (int)(this.centerX - this.count * partx) + X();
            int y = (int)(this.centerY - this.count * party) + Y();
            int width = (int)(this.centerX + this.count * partWidth);
            int height = (int)(this.centerY + this.count * partHeight);
            if (alpha > 0 && alpha < 1)
            {
                g.SetAlpha(alpha);
            }
            g.DrawTexture(this.texture, x, y, width, height);
            if (alpha > 0 && alpha < 1)
            {
                g.SetAlpha(1f);
            }
        }

        public void Reset()
        {
            this.complete = false;
            this.count = 0;
        }

        public int GetCenterX()
        {
            return centerX;
        }

        public void SetCenterX(int centerX)
        {
            this.centerX = centerX;
        }

        public int GetCenterY()
        {
            return centerY;
        }

        public void SetCenterY(int centerY)
        {
            this.centerY = centerY;
        }

        public int GetMaxCount()
        {
            return maxcount;
        }

        public void SetMaxCount(int maxcount)
        {
            this.maxcount = maxcount;
        }

        public LTexture GetBitmap()
        {
            return texture;
        }

        public RectBox GetCollisionBox()
        {
            return GetRect(x, y, width, height);
        }

        public bool IsVisible()
        {
            return visible;
        }

        public void SetVisible(bool visible)
        {
            this.visible = visible;
        }

        public void Dispose()
        {
            if (texture != null)
            {
                texture.Destroy();
                texture = null;
            }
        }
    }
}
